var express = require('express');
var router = express.Router();
var md5 = require("md5");
var fs = require("fs");


var multer = require("multer");
var arr = [];
var pic;
var storage = multer.diskStorage({
  destination:function(req,file,cb){
    cb(null,"./public/images/pic")
  },
  filename:function(req,file,cb){
    var suffix = file.mimetype.split("/")[1];
    pic = "http://192.168.15.249:3000/images/pic/"+file.filename+"-"+Date.now()+"."+suffix;
    arr.push(pic);
    cb(null,file.filename+"-"+Date.now()+"."+suffix);
  }
})

var upload = multer({storage:storage});

var userDAO = require("../bin/db/userDAO.js");
var goodDAO = require("../bin/db/goodDAO.js");
var cartDAO = require("../bin/db/cartDAO.js");



router.post("/regist",function(req,res){
  var reg = /^[\w|\u4e00-\u9fa5]{3,10}$/;
  if(!reg.test(req.body.username)){
    res.json({err:1,msg:"用户名不符合要求"});
    return;
  }
  reg = /^[0-9a-zA-Z]{4,12}$/;
  if(!reg.test(req.body.psw)){
    res.json({err:2,msg:"密码不符合要求"});
  }
  userDAO.findOne({username:req.body.username})
  .then(data=>{
    if(data){
      res.json({err:3,msg:"本用户名已注册"});
    }else{
      req.body.psw = md5(req.body.psw);
      var u = new userDAO(req.body);
      u.save()
      .then(data=>{
        res.json({err:0,msg:"注册成功，请前往登录页面"});
      })
    }
  })
})

router.post("/login",function(req,res){
  req.body.psw = md5(req.body.psw);
  userDAO.findOne(req.body)
  .then(data=>{
    if(!data){
      res.json({err:1,msg:"该用户名不存在或密码错误"});
      return;
    }else{
      req.session.user = data;
      res.json({err:0,msg:"登录成功",isLogin:true});
    }
  })
})

router.get("/exit",function(req,res){
  req.session.user = "";
  res.json({err:0,msg:"退出成功",isLogin:false});
})

router.get("/isLogin",(req,res)=>{
  if(req.session.user){
    res.json({err:0,isLogin:true});
  }
})

router.post("/upload",upload.array("pic"),(req,res)=>{
  req.body.pic = arr;
  userDAO.update({
    username:req.body.name,
    gender:req.body.gender,
    city:req.body.city,
    avatar:req.body.pic
  })
  .then(data=>{
    arr = [];
    res.json({err:0,msg:"上传成功",userInfo:req.session.user});
  })
})

router.post("/userInfo",(req,res)=>{
  if(req.session.user){
    userDAO.findOne({psw:req.session.user.psw})
    .then(data=>{
      res.json({err:0,msg:data});
    })
  }
})

router.post("/goodUpload",upload.array("pic"),(req,res)=>{
  req.body.pic = arr;
  var u = new goodDAO({
    name:req.body.name,
    price:req.body.price,
    area:req.body.area,
    num:req.body.num,
    picture:req.body.pic,
    type:req.body.type,
    owner:req.session.user._id
  })
  u.save()
  .then(data=>{
    arr = [];
    res.json({err:0,msg:"发布成功"});
  })
})

router.get("/detail",(req,res)=>{
  goodDAO.find({owner:req.session.user._id})
  .then(data=>{
    if(data.length>=1){
      res.json({have:true});
    }
  })
})

router.get("/mine",(req,res)=>{
  goodDAO.find({owner:req.session.user._id})
  .then(data=>{
    if(data.length>=1){
      res.json({msg:data});
    }
  })
})

router.post("/item",(req,res)=>{
  goodDAO.find({type:req.body.e})
  .then(data=>{
    res.json({msg:data});
  })
})

router.post("/goodSearch",(req,res)=>{
  var b = req.body.word;
  var arr = [];
  goodDAO.find({type:{$regex:b}})
  .then(data=>{
    b = "";
    data.forEach(function(a,b){
      if(arr.indexOf(a)<0){
        arr.push(a);
      }
    })
    res.json({msg:arr});
  })
})

router.post("/one",(req,res)=>{
  goodDAO.find({name:req.body.name})
  .then(data=>{
    res.json({msg:data});
  })
})

router.post("/add",(req,res)=>{
  cartDAO.findOne({name:req.body.goodInfo.name})
  .then(data=>{
    if(data){
      data.number++;
      res.json({msg:"添加成功"});
    }else{
      var u = new cartDAO({
        name:req.body.goodInfo.name,
        price:req.body.goodInfo.price,
        area:req.body.goodInfo.area,
        number:req.body.number,
        type:req.body.goodInfo.type,
        check:true,
        pic:req.body.goodInfo.picture,
        owner:req.session.user._id
      })
      u.save()
      .then(data=>{
        res.json({err:0,msg:"添加购物车成功"});
      })
    }
  })
})

router.post("/mycart",(req,res)=>{
  cartDAO.find({owner:req.session.user._id})
  .then(data=>{
    if(!data){
      return;
    }else{
      res.json({err:0,msg:data});
    }
  })
})

router.get("/remove",(req,res)=>{
  cartDAO.remove({_id:req.query.e})
  .then(data=>{
    res.json({err:0,msg:"删除成功"});
  })
})

router.get("/delete",(req,res)=>{
  goodDAO.findOne({_id:req.query.id})
  .then(data=>{
    for(var i = 0;i<data.picture.length;i++){
      var picName = data.picture[i];
      fs.unlinkSync("./public/images/pic/"+picName);
    }
    return goodDAO.remove({_id:req.query.id});
  })
  .then(()=>{
    res.json({err:0,msg:"删除成功"});
  })
})


router.post("/sort",(req,res)=>{
  if(req.body.a==-1){
    goodDAO.find({name:req.body.name})
    .sort(req.body.a)
    .then(data=>{
      res.json({msg:data});
    })
  }else if(req.body.a ==1){
    goodDAO.find({name:req.body.name})
    .sort(req.body.a)
    .then(data=>{
      res.json({msg:data});
    })
  }
})

module.exports = router;
